package Dao;

import java.sql.*;
import java.util.Scanner;

public class jdbcUtil {
    private static Connection con;
    private static Statement state;
    private static ResultSet rs;
    private static String sql;
    private static String URL= "jdbc:mysql://localhost:3306/小区管理系统?characterEncoding=GBK&&useSSL=false";
    private static String user="root";
    private static String password="123456";
    //连接数据库
    public static void Connect() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(URL, user, password);
    }
    //释放资源
    public static void close(){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(state!=null){
            try {
                state.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(con!=null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    //增删改方法
    public static void addData() throws ClassNotFoundException, SQLException {
        Connect();
        Scanner in =new Scanner(System.in);
        System.out.println("请输入你要增加记录的的表");
        String t=in.next();
        System.out.println("请输入你要增加的记录,并且用逗号隔开");
        String d=in.next();
        state = con.createStatement();
        sql="insert into "+t+" values("+d+")";
        int i=state.executeUpdate(sql);
        if(i == 0){
            System.out.println("操作失败");
        }else{
            System.out.println("操作成功");
        }
        close();
    }
    public static void deleteData() throws ClassNotFoundException, SQLException {
        Connect();
        Scanner in =new Scanner(System.in);
        System.out.println("请输入你要删除记录的的表");
        String t=in.next();
        System.out.println("请输入你要删除的记录的位置");
        String d=in.next();
        state = con.createStatement();
        sql="delete from "+t+" where "+d;
        int i=state.executeUpdate(sql);
        if(i == 0){
            System.out.println("操作失败");
        }else{
            System.out.println("操作成功");
        }
        close();
    }
    public static void updata() throws ClassNotFoundException, SQLException {
        Connect();
        Scanner in =new Scanner(System.in);
        System.out.println("请输入要修改数据的表");
        String t=in.next();
        System.out.println("请输入要修改数据的位置");
        String p=in.next();
        System.out.println("请输入你要修改的数据");
        String d=in.next();
        state = con.createStatement();
        sql="update "+t+" set "+d+" where "+p;
        int i=state.executeUpdate(sql);
        if(i == 0){
            System.out.println("操作失败");
        }else{
            System.out.println("操作成功");
        }
        close();
    }
    //业主表
    public static void queryYZ() throws ClassNotFoundException, SQLException {
            Connect();
            sql="select * from 业主";
            state = con.createStatement();
            rs=state.executeQuery(sql);
            System.out.println("业主表");
             while(rs.next()) {
                String a = rs.getString(1);
                String b = rs.getString(2);
                String c = rs.getString(3);
                String d = rs.getString(4);
                String e = rs.getString(5);
                String f = rs.getString(6);
                System.out.printf("%s\t%s\t%s\t%s\t%s\t%s\t",a,b,c,d,e,f);
                System.out.println('\n');
            }
            close();
    }
    //房屋表
    public static void queryFW() throws ClassNotFoundException, SQLException {
        Connect();
        sql="select * from 房屋";
        state = con.createStatement();
        rs=state.executeQuery(sql);
        System.out.println("房屋表");
        while(rs.next()) {
            String a = rs.getString(1);
            String b = rs.getString(2);
            Double c = rs.getDouble(3);
            int    d = rs.getInt(4);
            String e = rs.getString(5);
            System.out.printf("%s\t%s\t%1f\t%d\t%s\t",a,b,c,d,e);
            System.out.println('\n');
        }
        close();
    }
    //管理表
    public static void queryGL() throws ClassNotFoundException, SQLException {
        Connect();
        sql="select * from 管理";
        state = con.createStatement();
        rs=state.executeQuery(sql);
        System.out.println("管理表");
        while(rs.next()) {
            String a = rs.getString(1);
            String b = rs.getString(2);
            System.out.printf("%s\t%s\t",a,b);
            System.out.println('\n');
        }
        close();
    }
    //管理情况表
    public static void queryGLQK() throws ClassNotFoundException, SQLException {
        Connect();
        sql="select * from 管理情况";
        state = con.createStatement();
        rs=state.executeQuery(sql);
        System.out.println("管理情况表");
        while(rs.next()) {
            String a = rs.getString(1);
            String b = rs.getString(2);
            String c = rs.getString(3);
            Date d = rs.getDate(4);
            String e = rs.getString(5);
            String f = rs.getString(6);
            String g = rs.getString(7);
            System.out.printf("%s\t%s\t%s\t%tb\t%s\t%s\t%s",a,b,c,d,e,f,g);
            System.out.println('\n');
        }
        close();
    }
    //管理员表
    public static void queryGLY() throws ClassNotFoundException, SQLException {
        Connect();
        sql="select * from 管理员";
        state = con.createStatement();
        rs=state.executeQuery(sql);
        System.out.println("管理员表");
        while(rs.next()) {
            String a = rs.getString(1);
            String b = rs.getString(2);
            String c = rs.getString(3);
            String d = rs.getString(4);
            String e = rs.getString(5);
            System.out.printf("%s\t%s\t%s\t%s\t%s",a,b,c,d,e);
            System.out.println('\n');
        }
        close();
    }
    //物业费信息表
    public static void queryWYFXX() throws ClassNotFoundException, SQLException {
        Connect();
        sql="select * from 物业费信息";
        state = con.createStatement();
        rs=state.executeQuery(sql);
        System.out.println("物业费信息表");
        while(rs.next()) {
            String a = rs.getString(1);
            String b = rs.getString(2);
            Date   c = rs.getDate(3);
            int    d = rs.getInt(4);
            Double e = rs.getDouble(5);
            System.out.printf("%s\t%s\t%tb\t%d\t%1f",a,b,c,d,e);
            System.out.println('\n');
        }
        close();
    }
    //楼宇表
    public static void queryLY() throws ClassNotFoundException, SQLException {
        Connect();
        sql="select * from 楼宇";
        state = con.createStatement();
        rs=state.executeQuery(sql);
        System.out.println("楼宇表");
        while(rs.next()) {
            String a = rs.getString(1);
            int    b = rs.getInt(2);
            Double c = rs.getDouble(3);
            System.out.printf("%s\t%d\t%1f",a,b,c);
            System.out.println('\n');
        }
        close();
    }

}

